package com.wyp.didishige.activity.im;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.wyp.didishige.R;
import com.wyp.didishige.activity.BaseActivity;
import com.wyp.didishige.activity.dynamic.DynamicDetailActivity;
import com.wyp.didishige.adapter.NewCommentAdapter;
import com.wyp.didishige.config.Constants;
import com.wyp.didishige.db.BlackUser;
import com.wyp.didishige.db.BlackUserManager;
import com.wyp.didishige.db.NewComment;
import com.wyp.didishige.db.NewCommentManager;
import com.wyp.didishige.utils.IntentUtil;
import com.wyp.didishige.wiget.dialog.DialogTips;

public class CommentNotifyActivity extends BaseActivity {

    private ListView listNear;

    private TextView noData;
    private NewCommentAdapter newCommentAdapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_comment_notify);
        init();
    }

    @Override
    protected void initParamter() {
        super.initParamter();
        newCommentAdapter=new NewCommentAdapter(this,NewCommentManager.getInstance(this)
                .getAllNewComment());
    }

    @Override
    protected void initWidget() {
        super.initWidget();
        actionBar = getSupportActionBar();
        assert actionBar != null;
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);
        actionBar.setTitle("评论提醒");
        listNear = (ListView) findViewById(R.id.list_near);
        noData = (TextView) findViewById(R.id.noData);
    }

    @Override
    protected void initWidgetState() {
        super.initWidgetState();
        listNear.setAdapter(newCommentAdapter);
    }

    @Override
    protected void initWidgetListener() {
        super.initWidgetListener();
        listNear.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                NewComment comment= (NewComment) parent.getItemAtPosition(position);
                Bundle bundle=new Bundle();
                bundle.putString(Constants.DYNAMIC_ID,comment.getDynamicId());
                bundle.putString(Constants.COMMENT_ID,comment.getCommentId());
                IntentUtil.start_activity(CommentNotifyActivity.this,DynamicDetailActivity.class,bundle);
            }
        });
        listNear.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
                NewComment comment= (NewComment) parent.getItemAtPosition(position);
                showRemoveBlackDialog(position,comment);
                return true;
            }
        });
    }
    /** 显示移除黑名单对话框
     */
    public void showRemoveBlackDialog(final int position, final NewComment comment) {
        DialogTips dialog = new DialogTips(this, "删除评论",
                "你确定将此条评论删除吗?", "确定", true, true);
        // 设置成功事件
        dialog.SetOnSuccessListener(new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialogInterface, int userId) {
                newCommentAdapter.remove(position);
                NewCommentManager.getInstance(CommentNotifyActivity.this).deleteNewComment(comment);
                if (newCommentAdapter.getCount()==0){
                    noData.setText("暂无新评论");
                    noData.setVisibility(View.VISIBLE);
                }else{
                    noData.setVisibility(View.GONE);
                }
            }
        });
        // 显示确认对话框
        dialog.show();
    }
    @Override
    public void onResume() {
        super.onResume();
        newCommentAdapter.setList(NewCommentManager.getInstance(this)
                .getAllNewComment());
        newCommentAdapter.notifyDataSetChanged();
        if (newCommentAdapter.getCount()==0){
            noData.setText("暂无新评论");
            noData.setVisibility(View.VISIBLE);
        }else{
            noData.setVisibility(View.GONE);
        }
    }
}
